import request from '@/utils/request'

/**
 * 消息相关API
 */

// 发送消息
export function sendMessage(data) {
  return request.post('/message/send', data)
}

// 发送系统消息
export function sendSystemMessage(data) {
  return request.post('/message/system', data)
}

// 标记消息为已读
export function markMessageAsRead(messageId) {
  return request.post(`/message/${messageId}/read`)
}

// 批量标记为已读
export function batchMarkAsRead(messageIds) {
  return request.post('/message/batch-read', messageIds)
}

// 标记会话为已读
export function markConversationAsRead(conversationId) {
  return request.post(`/message/conversation/${conversationId}/read`)
}

// 删除消息
export function deleteMessage(messageId) {
  return request.delete(`/message/${messageId}`)
}

// 获取消息详情
export function getMessageDetail(messageId) {
  return request.get(`/message/${messageId}`)
}

// 分页查询消息列表
export function getMessagePage(params) {
  return request.get('/message/page', { params })
}

// 获取会话消息
export function getConversationMessages(conversationId, limit = 50) {
  return request.get(`/message/conversation/${conversationId}`, { 
    params: { limit } 
  })
}

// 获取未读消息
export function getUnreadMessages() {
  return request.get('/message/unread')
}

// 统计未读消息数量
export function countUnreadMessages() {
  return request.get('/message/unread/count')
}

// 统计会话未读数量
export function countUnreadByConversation(conversationId) {
  return request.get(`/message/conversation/${conversationId}/unread/count`)
}

// 获取系统消息
export function getSystemMessages(limit = 20) {
  return request.get('/message/system', { params: { limit } })
}

// 获取最近聊天用户
export function getRecentChatUsers(limit = 10) {
  return request.get('/message/recent-chats', { params: { limit } })
}

// 获取在线用户
export function getOnlineUsers() {
  return request.get('/message/online-users')
}

// 检查用户在线状态
export function isUserOnline(userId) {
  return request.get(`/message/user/${userId}/online`)
}

// 清理过期消息
export function cleanExpiredMessages(days = 30) {
  return request.delete('/message/clean', { params: { days } })
}

// ========== 会话相关API ==========

// 创建私聊会话
export function createPrivateConversation(targetUserId) {
  return request.post('/conversation/private', null, { 
    params: { targetUserId } 
  })
}

// 创建群聊会话
export function createGroupConversation(data) {
  return request.post('/conversation/group', data)
}

// 获取会话详情
export function getConversationDetail(conversationId) {
  return request.get(`/conversation/${conversationId}`)
}

// 分页查询会话列表
export function getConversationPage(params) {
  return request.get('/conversation/page', { params })
}

// 获取会话列表
export function getConversationList() {
  return request.get('/conversation/list')
}

// 获取私聊会话
export function getPrivateConversation(targetUserId) {
  return request.get(`/conversation/private/${targetUserId}`)
}

// 更新会话信息
export function updateConversation(conversationId, data) {
  return request.put(`/conversation/${conversationId}`, null, { 
    params: data 
  })
}

// 添加会话参与者
export function addParticipant(conversationId, userId) {
  return request.post(`/conversation/${conversationId}/participants`, null, { 
    params: { userId } 
  })
}

// 移除会话参与者
export function removeParticipant(conversationId, userId) {
  return request.delete(`/conversation/${conversationId}/participants/${userId}`)
}

// 获取会话参与者
export function getParticipants(conversationId) {
  return request.get(`/conversation/${conversationId}/participants`)
}

// 检查参与者权限
export function checkParticipant(conversationId) {
  return request.get(`/conversation/${conversationId}/check-participant`)
}

// 删除会话
export function deleteConversation(conversationId) {
  return request.delete(`/conversation/${conversationId}`)
}
